{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 40 可视化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【填空】假设已经将包matplotlib.pyplot导入并取别名为plt，请依次写出可添加图名、x轴名称、y轴名称以及图例的函数名。（ ）（ ）（ ）（ ）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：plt.title() plt.xlabel() plt.ylabel() plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【编程】请用matplotlib画出 y=sin(x^2)*cos(x^2) 的图表，x轴的取值范围在[-10,10]之间，并加上图名以及x/y轴的名称。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "`import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "x = np.linspace(-10, 10)\n",
    "y = np.power(np.sin(x), 2) *np.power(np.cos(x), 2)\n",
    "plt.plot(x, y)\n",
    "plt.title(\"y=sin(x^2)*cos(x^2)\")\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.xlim(-10,10)\n",
    "plt.show()`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【编程】假设y1=[23,8,11,30,55,33,27,15,21,19,25,38,29,15,17]，y2=[18,8,3,26,40,49,50,33,27,25,29,41,45,32,26]，请在一个图中画出这两条折线，其中x轴表示值的个数，y轴表示y1/y2值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "`import matplotlib.pyplot as plt \n",
    "x1=range(0,15) \n",
    "x2=range(0,15) \n",
    "y1=[23,8,11,30,55,33,27,15,21,19,25,38,29,15,17] \n",
    "y2=[18,8,3,26,40,49,50,33,27,25,29,41,45,32,26]\n",
    "plt.plot(x1,y1,label='Frist line',linewidth=1,color='g',marker='o',markerfacecolor='blue',markersize=3) \n",
    "plt.plot(x2,y2,label='second line',linewidth=1,color='r',marker='d',markerfacecolor='yellow',markersize=3) \n",
    "plt.xlabel('Plot Number') \n",
    "plt.ylabel('Value') \n",
    "plt.title('The line chart') \n",
    "plt.legend(loc='upper right') \n",
    "plt.show()`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【编程】请使用pandas的画图函数，画出如下所示数据的柱状图，使x轴代表income，y轴代表expense。  \n",
    "数据来源URL：http://data.stats.gov.cn/easyquery.htm?cn=E0103   \n",
    "`\n",
    "地区\tincome\texpense\n",
    "北京市\t57229.83\t37425.34\n",
    "天津市\t37022.33\t27841.38\n",
    "河北省\t21484.13\t15436.99\n",
    "山西省\t20420.01\t13664.44\n",
    "内蒙古自治区\t26212.23\t18945.54\n",
    "辽宁省\t27835.44\t20463.36\n",
    "吉林省\t21368.32\t15631.86\n",
    "黑龙江省\t21205.79\t15577.48\n",
    "上海市\t58987.96\t39791.85\n",
    "江苏省\t35024.09\t23468.63\n",
    "浙江省\t42045.69\t27079.06\n",
    "安徽省\t21863.3\t15751.74\n",
    "福建省\t30047.75\t21249.35\n",
    "江西省\t22031.45\t14459.02\n",
    "山东省\t26929.94\t17280.69\n",
    "河南省\t20170.03\t13729.61\n",
    "湖北省\t23757.17\t16937.59\n",
    "湖南省\t23102.71\t17160.4\n",
    "广东省\t33003.29\t24819.63\n",
    "广西壮族自治区\t19904.76\t13423.66\n",
    "海南省\t22553.24\t15402.73\n",
    "重庆市\t24152.99\t17898.05\n",
    "四川省\t20579.82\t16179.94\n",
    "贵州省\t16703.65\t12969.62\n",
    "云南省\t18348.34\t12658.12\n",
    "西藏自治区\t15457.3\t10320.12\n",
    "陕西省\t20635.21\t14899.67\n",
    "甘肃省\t16011\t13120.11\n",
    "青海省\t19001.02\t15503.13\n",
    "宁夏回族自治区\t20561.66\t15350.29\n",
    "新疆维吾尔自治区\t19975.1\t15087.3\n",
    "`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：\n",
    "`import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "data=pd.read_csv('data.csv',index_col=0,encoding='gbk')\n",
    "data.plot(kind=\"bar\",x=\"income\",y=\"expense\",color=\"blue\")\n",
    "plt.xlabel(\"income\")\n",
    "plt.ylabel(\"expense\")\n",
    "plt.legend(\" \")\n",
    "plt.show()`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【编程】利用seaborn的内置数据tips（餐厅小费情况），进行如下分析：  \n",
    "小费和总消费之间的关系（绘出散点图和回归方程）；  \n",
    "午饭和晚饭，哪一顿顾客更愿意给小费（绘出箱线图）；   \n",
    "男性顾客和女性顾客谁更愿意给小费（绘出小提琴图）。  \n",
    "提示：可利用“sns.load_dataset（）”载入内置数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：  \n",
    "`import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "tips = sns.load_dataset('tips')\n",
    "sns.regplot(x='total_bill', y='tip', data=tips)\n",
    "sns.boxplot(y='tip',x='time',data=tips)\n",
    "sns.violinplot(x='sex',y='tip',data=tips)`"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
